//
//  SelfTextView.m
//  AnnCar_user
//
//  Created by LiBenCan on 17/10/26.
//  Copyright © 2017年 LiBenCan. All rights reserved.
//

#import "SelfTextView.h"
@interface SelfTextView()<UITextViewDelegate>
@end
@implementation SelfTextView

- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text PlaceHodel:(NSString *)hodelStr
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        [self.layer setCornerRadius:3];
        self.layer.masksToBounds = YES;
        
        //输入框
        _placeStr = hodelStr;
        _TextV = [[UITextView alloc]initWithFrame:CGRectMake(8*screenW_6,8*screenW_6, frame.size.width-16*screenW_6,frame.size.height-16*screenW_6)];
        _TextV.backgroundColor = backColor;
        _TextV.font = [UIFont systemFontOfSize:16*screenW_6];
        _TextV.text = hodelStr;
        _TextV.delegate = self;
        _TextV.textColor = TextGray;
        [self addSubview:_TextV];
        if (text.length) {
            _TextV.text = text;
        }
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame PlaceHodel:(NSString *)hodelStr
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        [self.layer setCornerRadius:3];
        self.layer.masksToBounds = YES;
        
        //输入框
        _placeStr = hodelStr;
        _TextV = [[UITextView alloc]initWithFrame:CGRectMake(8*screenW_6,8*screenW_6, frame.size.width-16*screenW_6,frame.size.height-16*screenW_6)];
        _TextV.backgroundColor = [UIColor whiteColor];
        _TextV.font = [UIFont systemFontOfSize:16*screenW_6];
        _TextV.text = hodelStr;
        _TextV.delegate = self;
        _TextV.textColor = TextGray;
        
        UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0,0, SCREENW,44)];
        
        bar.backgroundColor = backColor;
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREENW - 60, 0,50, 44)];
        
        [button setTitle:@"完成"forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(textPrint) forControlEvents:UIControlEventTouchUpInside];
        
        [bar addSubview:button];
        
        _TextV.inputAccessoryView = bar;
        
        [self addSubview:_TextV];
    }
    return self;
}

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:_placeStr]) {
        textView.text = @"";
        textView.textColor = TextBlack;
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length<=0) {
        textView.text  = _placeStr;
        textView.textColor = TextGray;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        if (textView.text.length > 280) {
            textView.text = [textView.text substringToIndex:280];
        }
    }
    if (_blcok) {
        _blcok(textView.text);
    }
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    if (size.height <= 50*screenW_6) {
        size.height= 50*screenW_6;
    }
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, frame.size.width, size.height);
}

- (void)textPrint
{
    [_TextV resignFirstResponder];
}

@end
